package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.SearchParams;
import com.yuandian.api.customer_perception.po.OperatorOverviewParams;
import com.yuandian.api.management.entity.ConfBusinessHall;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfBusinessHallService;
import com.yuandian.management.vo.BusinessHallExcelVo;
import com.yuandian.management.vo.SearchOptionVo;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 营业厅配置表-Controller
 * @author zfsn
 * @date 2023-3-28
 */
@Api(tags = "外部指标管理-导入导出管理-营业厅信息")
@RestController
@RequestMapping("/businessHall")
public class ConfBusinessHallController {

	@Autowired
	private ConfBusinessHallService confBusinessHallService;


	/**
	 * 通过主键查询数据
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true, dataType = "Integer")
	})
	public R<ConfBusinessHall> queryById(@PathVariable("id") Integer id) {
		return R.ok(confBusinessHallService.getById(id));
	}

	/**
	 * 分页查询
	 * @param confBusinessHall 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "num", value = "营业厅编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "营业厅名称", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "fullRegionName", value = "区域名称", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Page<ConfBusinessHall>> page(@RequestBody @ApiIgnore ConfBusinessHall confBusinessHall) {
		return R.ok(confBusinessHallService.listPage(confBusinessHall));
	}

	/**
	 * 新增数据
	 * @param confBusinessHall 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@SysLog("新增营业厅配置表数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "num", value = "营业厅编号", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "营业厅名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaCode", value = "所属区域编码", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> save(@Validated({InsertFlag.class, Default.class}) @RequestBody @ApiIgnore ConfBusinessHall confBusinessHall) {
		return confBusinessHallService.saveBusinessHall(confBusinessHall);
	}

	/**
	 * 通过主键更新数据
	 * @param confBusinessHall 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新营业厅配置表数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "num", value = "营业厅编号", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "营业厅名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaCode", value = "所属区域编码", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> updateById(@RequestBody @Validated(UpdateFlag.class) ConfBusinessHall confBusinessHall) {
		return confBusinessHallService.updateBusinessHallById(confBusinessHall);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除营业厅配置表数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer")
	})
	public R<Boolean> removeById(@PathVariable("ids") List<Integer>  ids){
		return R.ok(confBusinessHallService.removeByIds(ids));
	}

	/**
	 * 指定地区下的营业厅
	 * @param searchParams 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "指定地区下的营业厅", notes = "指定地区下的营业厅")
	@PostMapping("/getBusinessHall")
	public R<List<Map<String, String>>> getBusinessHall(@RequestBody SearchParams searchParams){
		return R.ok(confBusinessHallService.getBusinessHall(searchParams));
	}

	/**
	 * 查询营业厅信息并装换成 Map<营业厅编码， 营业厅名称>
	 *
	 * @param searchParams 业务编码集合
	 * @return 搜索选项vo对象集合
	 */
	@ApiIgnore
	@PostMapping("/getBusinessHallMap")
	public R<Map<String, String>> getBusinessHallMap(@RequestBody SearchParams searchParams){
		return R.ok(confBusinessHallService.getBusinessHallMap(searchParams));
	}

	/**
	 * 根据营业厅名称列表获取营业厅Id列表
	 *
	 * @param businessHallName
	 * @return 搜索选项vo对象集合
	 */
	@ApiIgnore
	@PostMapping("/getBusinessHallIdListByHallNameList")
	public R<List<String>> getBusinessHallIdListByHallNameList(String businessHallName) {
		return R.ok(confBusinessHallService.getBusinessHallIdListByHallNameList(businessHallName));
	}
	@ApiIgnore
	@GetMapping("/getBusinessHallIdListByHallNameList")
	public R<List<String>> getBusinessHallIdListByHallNameListCopy(String businessHallName) {
		return R.ok(confBusinessHallService.getBusinessHallIdListByHallNameList(businessHallName));
	}

	/**
	 * 营业厅信息模板
	 *
	 * @param response response
	 * @throws IOException
	 */
	@GetMapping("/downloadTemplate")
	@ApiOperation(value = "营业厅信息模板下载", notes = "营业厅信息模板下载")
	public void getTemplate(HttpServletResponse response) throws IOException {
		ExcelUtil.writeExcel(BusinessHallExcelVo.class, "营业厅信息模板", "营业厅信息模板", new ArrayList<>(), response);
	}


	/**
	 * 上传
	 *
	 * @param file file
	 * @return Result
	 * @throws IOException
	 */
	@PostMapping("/upload")
	@ApiOperation(value = "营业厅信息数据上传", notes = "营业厅信息数据上传")
	public R upload(@RequestParam("file") MultipartFile file) throws IOException {
		Map<String, List<BusinessHallExcelVo>> resultMap = ExcelUtil.readAndSaveExcelData(file, 0, 2, BusinessHallExcelVo.class, confBusinessHallService);

		List<BusinessHallExcelVo> successList = resultMap.get("result");
		List<BusinessHallExcelVo> failList = resultMap.get("fail");
		if (!CollectionUtils.isEmpty(successList) && CollectionUtils.isEmpty(failList)) {
			return R.ok("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
		}

		return R.failed("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
	}


	@PostMapping("/queryBusinessHall")
	@ApiOperation(value = "查询指定地区下的营业厅", notes = "查询指定地区下的营业厅")
	public R<List<SearchOptionVo>> queryBusinessHall(@RequestBody OperatorOverviewParams params){
		List<SearchOptionVo> searchOptionVos = confBusinessHallService.queryBusinessHall(params);
		return R.ok(searchOptionVos);
	}

	/**
	 * 根据营业厅编码查询营业厅名
	 * @return
	 */
	@GetMapping("/queryBusinessHallNameById")
	public R<String> queryBusinessHallNameById(@RequestParam String businessHallId){
		return confBusinessHallService.queryBusinessHallNameById(businessHallId);
	}
}
